home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14075 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Date: 11 Apr 1996 10:40:00 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4kjg5gINNe3a@anvil.ugrad.cs.ubc.ca>
  8. References: <31616F63.481D@lava.weeg.uiowa.edu> <4jtddt$eu7@masala.cc.uh.edu> <DpBuF6.83C@ukpsshp1.serigate.philips.nl> <3169994D.665ACF69@cs.ucl.ac.uk>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <3169994D.665ACF69@cs.ucl.ac.uk>,
  12. Bill Zissimopoulos  <B.Zissimopoulos@cs.ucl.ac.uk> wrote:
  13. >Stephen Baynes wrote:
  14. >> 
  15. >> Spasmo (cosc19z5@Bayou.UH.EDU) wrote:
  16. >> : Artur Wojdat (awojdat@lava.weeg.uiowa.edu) wrote:
  17. >> : : Hello everybody,
  18. >> : :     Is there a function or some sort of way that I could remove '\n'
  19. >> : : charecter form the end of the string. I'm reading from two files, want to
  20. >> : Dunno if there are any functions available, but what I always do
  21. >> : is just overwrite the '\n' with a '\0', which does the job nicely.
  22. >> 
  23. >> : For example, let's say that the string that holds the data is called
  24. >> : buf.  To get rid of the '\n' you'd merely do the following:
  25. >> 
  26. >> :       buf[strlen(buf) - 1] = '\0';
  27. >> 
  28. >> : And that takes care of that.
  29. >> 
  30. >> Thats the best way, but do check that the character you are about to overwrite
  31. >> is a newline. There are two occasions when fgets stops without reading a
  32. >> newline, first if it has filled your buffer (because you have a very long line
  33. >> in the file), second if there is no newline at the end of the last line of the
  34. >> file.
  35. >
  36. >Which makes this the worst way for the exact reasons that you give.
  37.  
  38. In fact, if one can still trace back to the original article that started this
  39. thread, any solutions to the problem involving buffers, string manipulationa nd
  40. other buffoonery are completely wacky.
  41.  
  42. The original question, part of which is reveled in the quote above, calls for
  43. a program that will read lines from two files and display them side by side on
  44. the standard output---clearly a trivial problem.
  45.  
  46. This problem can be solved by scanning lines character by character alternating
  47. between the the left and the right file, and putting them onto standard output
  48. using just the getc() and putchar() macros. You read until a newline or EOF
  49. from one file and print, but don't print the newline.  Then you do the same
  50. thing from the second file,  and output a newline. If neither file is at EOF,
  51. you repeat the process. If one file is at EOF but the other is not, you then
  52. dump the rest of the remaining file to standard output.
  53.  
  54. Solutions to this problem involving strlen(), fgets() and other zoological
  55. beasts show a lack of focus and clarity of thought.  I recommend a good dose of
  56. meditation each day, a sound diet, and mandatory readings from a suitable bible
  57. (a tome dedicated to some area of computer science or another, of course).
  58.  
  59. The fact is that the problem requires _zero_ input symbols of _lookahead_ in
  60. either file, hence buffering is only needed for I/O efficiency, not for
  61. isolating tokens (in this case lines). The efficiency buffering is already
  62. done by the standard IO library. Further buffering in the application code (in
  63. this case) is wasteful of memory and CPU cycles, and  results in an
  64. implementation that is unnecessarily complex and hard to read.
  65.  
  66. For example, the check whether the result of fgets() has a newline or not is
  67. completely wasteful, and is needed in order to hack what is a poor design in
  68. the first place into working properly.   I agree with you completely about this
  69. being a bad approach, if not the worst.
  70. -- 
  71.  
  72.